/*
 * Created on 2006-7-7
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.powerunion.datacollection.report.excelreport.datasource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * 数据库连接对象。
 * @author juny
 */
public class DBConnection {
    /*public DBConnection(){
        ;
    }*/
    
    public DBConnection(String jndiName){
        this.jndiName = jndiName;
    }
    
    public DBConnection(String driverClassName, String url, String userName, String psw){
        this.driverClassName = driverClassName;
        this.url = url;
        this.userName = userName;
        this.psw = psw;
    }
    
    private boolean validJNDI = false;
    private boolean checkJNDI(){
        if(this.validJNDI){
            return true;
        }
        
        if(null != jndiName && !"".equals(jndiName)){
            this.validJNDI = true;
            return true;
        }
        
        this.validJNDI = false;
        return false;
    }
    
    private boolean validJDBC = false;
    private boolean checkJDBC(){
        if(this.validJDBC){
            return true;
        }
        
        if(null == this.driverClassName || "".equals(this.driverClassName)){
            this.validJDBC = false;
            return false;
        }
        
        if(null == this.url || "".equals(this.url)){
            this.validJDBC = false;
            return false;
        }
        
        if(null == this.userName || "".equals(this.userName)){
            this.validJDBC = false;
            return false;
        }
        
        if(null == this.psw || "".equals(this.psw)){
            this.validJDBC = false;
            return false;
        }
        this.validJDBC = true;
        return true;
    }
    
    public boolean testConnection(){
        return true;
    }
    
    public Connection getConnection() throws Exception{
        if(this.checkJNDI()){
            return getDataSourceFromJNDI();
        }
        
        if(this.checkJDBC()){
            return getDataSourceFromJDBC();
        }

        throw new Exception("You must define the database connection paramaters first " +
        	"before you invok the function getConnection().");
        
    }
    
    public Connection getDataSourceFromJNDI() throws NamingException, SQLException{
        if(null == ds){
	        InitialContext ctx = new InitialContext();
	    	ds = (DataSource)ctx.lookup(jndiName);
        }
        return ds.getConnection();
    }
    
    public Connection getDataSourceFromJDBC() throws ClassNotFoundException, SQLException{
        if(!this.initializedDriverClass){
            Class.forName(this.driverClassName);
            this.initializedDriverClass = true;
        }
        
        return DriverManager.getConnection(
                this.url,
                this.userName, 
                this.psw);
    }
    
    private String jndiName = null;
    private String driverClassName = null;
    private String url = null;
    private String userName = null;
    private String psw = null;
    private DataSource ds = null;
    private boolean initializedDriverClass = false;
}
